﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using ZillowApiClient.Data.Model;

namespace ZillowApiClient
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Mapper.Initialize(m => m.CreateMap<InputRecord, OutputRecord>());
            Start();
            Console.WriteLine("Press <ENTER> to exit.");
            Console.ReadLine();
            // MainMenu();
        }

        private static void Start()
        {
            try
            {
                var proc = new BatchZillowProcess();
                var config = new AppConfig();

                Console.WriteLine("Zillow API Batch Zestimate. 2015 by Sunny Kolattukudy.");
                Console.WriteLine("<API Settings>");
                Console.WriteLine($"Zillow API Id: {config.ZillowApiId}");
                Console.WriteLine($"Pause after {config.PauseInterval} calls.");
                Console.WriteLine();
                Console.WriteLine("<File Settings>");
                Console.WriteLine($"Input file location : {config.InputPath}");
                Console.WriteLine($"Output file location : {config.OutputPath}");
                Console.WriteLine($"Input # header lines: {config.InputHeaderLines}");
                Console.WriteLine($"Output include column headings : {config.IncludeOutputHeader}");
                //Configure();
                Console.WriteLine();
                Console.WriteLine("Edit ZillowApiClient.config located in the EXE's folder to edit these values.");
                Console.WriteLine("Press <ENTER> to run with these settings.");
                Console.ReadLine();
                proc.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"There was a problem: {ex.Message}");
            }
        }

        //private static void MainMenu()
        //{
        //    var menuOptions = new List<string>() { "options", "start", "quit" };
        //    string choice = string.Empty;
        //    while (!choice.Equals("quit"))
        //    {
        //        choice = Prompt("What would you like to do? \r\n START a new batch retrieval from Zillow \r\n Change your OPTIONS \r\n Or QUIT", menuOptions);
        //        if (choice.Equals("start"))
        //            Start();
        //        if (choice.Equals("options"))
        //            OptionsMenu();
        //    }
        //}

        //private static void OptionsMenu()
        //{
        //    var menuOptions = new List<string>() { "InputPath", "OutputPath", "PauseInterval" };
        //    var menuOptionText = menuOptions.Aggregate((x, y) => x + ", " + y) + ". You can also type \"Back\" to the main menu.";
        //    menuOptions.Add("Back");

        //    string choice = string.Empty;
        //    while (!choice.Equals("Back"))
        //    {
        //        choice = Prompt($"What would you like to change? Your options are: {menuOptionText}", menuOptions);
        //        if (choice.Equals("Back"))
        //            return;
        //        ConfigurationManager.AppSettings.Remove(choice);
        //        Configure();
        //    }
        //}

        private static string Prompt(string promptText, List<string> menuOptions)
        {
            var inputString = string.Empty;
            while (string.IsNullOrWhiteSpace(inputString) || menuOptions.All(x => !x.Equals(inputString)))
            {
                Console.WriteLine(promptText);
                inputString = Console.ReadLine();
            }
            return menuOptions.First(x => x.Equals(inputString));
        }

        //private static string PromptForConfigValue(string key, string prompt = null)
        //{
        //    var inputString = string.Empty;
        //    while (string.IsNullOrWhiteSpace(inputString))
        //    {
        //        Console.WriteLine(prompt ?? $"Enter configuration value for {key}:");
        //        inputString = Console.ReadLine();
        //    }
        //    Console.WriteLine("Value set. You can change this setting manually in ZillowApiClient.config");
        //    return inputString;
        //}

        //internal static void Configure()
        //{
        //    var Config = new AppConfig();
        //    if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings.Get("ZillowApiId")))
        //        Config.ZillowApiId = PromptForConfigValue("ZillowApiId");
        //    if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings.Get("InputPath")))
        //        Config.InputPath = PromptForConfigValue("InputPath");
        //    if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings.Get("InputHeaderLines")))
        //        Config.InputHeaderLines = int.Parse(PromptForConfigValue("InputHeaderLines", "How many header lines to ignore? (0 for none)"));
        //    if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings.Get("PauseInterval")))
        //        Config.PauseInterval = int.Parse(PromptForConfigValue("PauseInterval",
        //            "How many times can I call the API without pausing?"));
        //    if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings.Get("OutputPath")))
        //        Config.OutputPath = PromptForConfigValue("OutputPath");
        //    if (string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings.Get("IncludeOutputHeader")))
        //        Config.IncludeOutputHeader = ("true, T, Y".Contains(PromptForConfigValue("IncludeOutputHeader", "Include the column headers on output? Type \"true\" or \"false\".")));
        //}
    }
}